<?php
/**
 * Transaction
 *
 * Description de la classe
 *
 * @author Mathieu
 */

class Transaction extends Object {
    protected $id;
    protected $source_id;
    protected $state;
    protected $code;
    protected $error;
    protected $merchant_id;
    protected $country_merchant;
    protected $amount;
    protected $payment_means;
    protected $complementary_code;
    protected $payment_time;
    protected $payment_date;
    protected $response_code;
    protected $payment_certificate;
    protected $authorisation_id;
    protected $currency_code;
    protected $card_number;
    protected $cvv_flag;
    protected $cvv_response_code;
    protected $bank_response_code;
    protected $complementary_info;
    protected $return_context;
    protected $caddie;
    protected $receipt_complement;
    protected $merchant_language;
    protected $language;
    protected $customer_id;
    protected $order_id;
    protected $customer_email;
    protected $customer_ip_address;
    protected $capture_day;
    protected $capture_mode;
    protected $data;
    protected $revenue_sharing;

    protected $user;

    function __construct($id="", $params=array()) {
        if($id != "") {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }
    function load(){
         $db = Database::getInstance();
        $sql = "SELECT * FROM transactions where id='".$this->id."'";
        $result = $db->query($sql);
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }
    function getUser() {
        if(isset($this->user)) return $this->user;
        return new User($this->customer_id);
    }
    function getProducts($vendor_id) {
		
    }
    function getVendors() {
         $db = Database::getInstance();
		 $sql="	SELECT DISTINCT psp.vendor_id FROM  panier_sell_produit psp
				INNER JOIN paniers_sell ps ON psp.panier_sell_id=ps.id
				INNER JOIN transactions t ON t.source_id=ps.id
				WHERE t.id='".$this->id."'
				";
		  $result = $db->query($sql);
		  $vendors=array();
		if(sizeof($result) > 0) {
            foreach($result as $res) {
				$vendor =new Vendor($res['vendor_id']);
				$vendor->load();
				$vendors[]=$vendor;
            }
			
            return $vendors;
        }  
    }
    function save() {
		$db = Database::getInstance();
        if(isset($this->id)){
			$sql = "UPDATE transactions SET
					source_id           ='".$this->source_id."',
					state               ='".$this->state."',
					code                ='".$this->code."',
					error               ='".$this->error."',
					merchant_id         ='".$this->merchant_id."',
					country_merchant    ='".$this->country_merchant."',
					amount              ='".$this->amount."',
					payment_means       ='".$this->payment_means."',
					transmission_date   ='".$this->transmission_date."',
					payment_time        ='".$this->payment_time."',
					payment_date        ='".$this->payment_date."',
					response_code       ='".$this->response_code."',
					payment_certificate ='".$this->payment_certificate."',
					authorisation_id    ='".$this->authorisation_id."',
					currency_code       ='".$this->currency_code."',
					card_number         ='".$this->card_number."',
					cvv_flag            ='".$this->cvv_flag."',
					cvv_response_code   ='".$this->cvv_response_code."',
					bank_response_code  ='".$this->bank_response_code."',
					complementary_code  ='".$this->complementary_code."',
					complementary_info  ='".$this->complementary_info."',
					return_context      ='".$this->return_context."',
					caddie              ='".$this->caddie."',
					receipt_complement  ='".$this->receipt_complement."',
					merchant_language 	='".$this->merchant_language."',
					language            ='".$this->language."',
					customer_id         ='".$this->customer_id."',
					order_id            ='".$this->order_id."',
					customer_email      ='".$this->customer_email."',
					customer_ip_address ='".$this->customer_ip_address."',
					capture_day         ='".$this->capture_day."',
					capture_mode        ='".$this->capture_mode."',
					data                ='".$this->data."',
					revenue_sharing     ='".$this->revenue_sharing."'
					WHERE id='".$this->id."'";
			$db->query($sql);
        } else {
        	$sql = "INSERT INTO transactions 
		 					(state, 
							 customer_id, 
							 customer_email, 
							 customer_ip_address, 
							 revenue_sharing) 
		 			   VALUES('".$this->state."',
					   		   '".$this->user['id']."',
							   '".$this->user['email']."',
							   '".$_SERVER['REMOTE_ADDR']."',
							   '".$this->revenue_sharing."')";
         $db->query($sql);
         $this->id= $db->getInsertedId();
        }
    }

}